package com.ssm.modules.controller;

import com.ssm.common.bean.VResponse;
import com.ssm.modules.entity.Problem;
import com.ssm.modules.entity.User;
import com.ssm.modules.service.IProblemService;
import com.ssm.modules.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @email:
 * @author:
 * @date: 2018/12/18
 * @time: 17:26
 * 问题Controller
 */
@Controller
@RequestMapping("problem")
public class ProblemController {
    @Autowired
    private IProblemService problemService;
    @Autowired
    private IUserService userService;

    @ResponseBody
    @RequestMapping(value = "/selectProblem",method = RequestMethod.GET)
    public VResponse<List<Problem>> selectProblem(){
        List<Problem> problems = problemService.selectAll();
        return VResponse.success(problems);
    }

    @ResponseBody
    @RequestMapping(value = "/addProblem",method = RequestMethod.POST)
    public VResponse<Object>addProblem(@RequestBody Problem problem){
        try {
            problemService.addProblem(problem);
            return VResponse.success("添加成功");
        }catch (Exception e) {
            return VResponse.error(0, "添加失败");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/updateProblem",method = RequestMethod.PUT)
    public VResponse<Object>updateProblem(@RequestBody Problem problem){
        this.problemService.updateProblem(problem);
        return VResponse.success("修改成功");
    }

    @ResponseBody
    @RequestMapping(value = "/deleteProblrm",method = RequestMethod.DELETE)
    public VResponse<Object>deleteProblem(@RequestParam("id") int id){
        this.problemService.deleteProblem(id);
        return VResponse.success("删除成功");
    }

    @RequestMapping(value = "feedback", method = RequestMethod.GET)
    public String feedback(@RequestParam(value = "page" ,defaultValue = "-1")int page,
                           @RequestParam(value = "pageSize" ,defaultValue = "15")int pageSize,
                           Map<String, Object> map,
                           HttpServletRequest request){
        try {
            User user = (User) request.getSession().getAttribute("user");
            if(user != null && user.getRole().getId() == 3) {
                List<User> allTea = userService.findAllTea();
                int stuId = user.getId();
                List<Problem> problems = problemService.findByStuId(stuId, page, pageSize);
                //  获取总数
                int amount = problemService.findByStuIdCount(stuId);

                map.put("allTea", allTea);
                map.put("problems", problems);
                map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));
            }else if(user != null && user.getRole().getId() == 2) {
                int teaId = user.getId();
                List<Problem> problems = problemService.findByTeaId(teaId, page, pageSize);
                //  获取总数
                int amount = problemService.findByTeaIdCount(teaId);
                map.put("problems", problems);
                map.put("pageItemSize", (int)Math.ceil(amount / (double)pageSize));
            }

            map.put("page", page);
            return "/stu/problem";
        }catch (Exception e) {

            return "/stu/problem";
        }
    }

    @ResponseBody
    @RequestMapping(value = "/replay",method = RequestMethod.PUT)
    public VResponse<Object> replay(@RequestBody Problem problem) {
        int id = problem.getId();
        String reply = problem.getReply();
        problemService.updateReplay(id, reply);
        return VResponse.success("回复成功");
    }

}
